package com.sanpu.kxe.business.repay.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.repay.adapter.RefundPlanAdapter;
import com.sanpu.kxe.business.repay.bean.RefundPlanInput;
import com.sanpu.kxe.business.repay.bean.RefundPlanOutput;
import com.sanpu.kxe.business.repay.contract.RefundPlanContract;
import com.sanpu.kxe.business.repay.contract.RepayConfig;
import com.sanpu.kxe.business.repay.interfaces.IRefundCountListener;
import com.sanpu.kxe.business.repay.interfaces.IRefundPlanListener;
import com.sanpu.kxe.business.repay.presenter.RefundPlanPresenter;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.common.utils.RegionCheckListener;
import com.sanpu.kxe.common.utils.SingleCheckListener;
import com.sanpu.kxe.module.dialog.RefundCountChooseDialog;
import com.sanpu.kxe.module.wheel.interfaces.IWheelDataChangeCallback;
import com.sanpu.kxe.module.wheel.interfaces.IWheelRegionChangeCallback;
import com.sanpu.kxe.utils.DialogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：还款详情
 * Created by 薛勇军 on 2019/4/10.
 */

public class RefundPlanActivity extends BaseTitleActivity implements RefundPlanContract.View {

    @Bind(R.id.planLv)
    ListView planLv;
    @Bind(R.id.containLayout)
    RelativeLayout containLayout;
    @Bind(R.id.refundStartIv)
    TextView refundStartIv;

    private TextView refundStartTv, refundTv, cardInfoTv, repayMoneyTv,
            cardBalanceTv, repayCountTv, repayChargeTv, addressTv, repayDayTv, overDueDayPromptTv;

    private LinearLayout addressLl, repayDayLl;

    private RefundPlanAdapter refundPlanAdapter;

    private RefundCountChooseDialog mCountChooseDialog;

    private RefundPlanContract.Presenter mPresenter;

    private SingleCheckListener singleCheckListener;

    private RegionCheckListener regionCheckListener;

    private List<RefundPlanOutput.RefundPlan> datas = new ArrayList<>();

    private String repayOrderNo, mWheelCode;

    private String mCurrentPaymentDays, maxPaymentDays, minPaymentDays;

    private boolean isOptionalMcc;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_refund_details;
    }

    @Override
    protected void initView() {
        super.initView();

        commonTitle.setTitle("还款计划");

        View headView = LayoutInflater.from(mContext).inflate(R.layout.view_refund_details_head, null);
        overDueDayPromptTv = ButterKnife.findById(headView, R.id.overDueDayPromptTv);
        refundStartTv = ButterKnife.findById(headView, R.id.refundStartTv);
        refundTv = ButterKnife.findById(headView, R.id.refundTv);
        cardInfoTv = ButterKnife.findById(headView, R.id.cardInfoTv);
        repayMoneyTv = ButterKnife.findById(headView, R.id.repayMoneyTv);
        cardBalanceTv = ButterKnife.findById(headView, R.id.cardBalanceTv);
        repayCountTv = ButterKnife.findById(headView, R.id.repayCountTv);
        repayChargeTv = ButterKnife.findById(headView, R.id.repayChargeTv);
        repayDayLl = ButterKnife.findById(headView, R.id.repayDayLl);
        repayDayTv = ButterKnife.findById(headView, R.id.repayDayTv);
        addressLl = ButterKnife.findById(headView, R.id.addressLl);
        addressTv = ButterKnife.findById(headView, R.id.addressTv);
        planLv.addHeaderView(headView);

        isOptionalMcc = getIntent().getBooleanExtra(RepayConfig.INTENT_ISOPTIONALMCC, false);

        refundPlanAdapter = new RefundPlanAdapter(new ArrayList<RefundPlanOutput.RefundPlan>(),
                mContext, isOptionalMcc, new IRefundPlanListener() {
            @Override
            public void checkBusiness(final int position) {
                if (!isOptionalMcc) return;
                List<String> selectList = mPresenter.getSelectList();
                if (selectList == null || selectList.size() == 0)
                    return;
                if (singleCheckListener == null) {
                    singleCheckListener = new SingleCheckListener(RefundPlanActivity.this);
                }
                singleCheckListener.handleCheck(containLayout, datas.get(position).getMccName(), selectList, new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(String wheelData) {
                        datas.get(position).setMccName(wheelData);
                        datas.get(position).setMccCode(mPresenter.getMccCodeByName(wheelData));
                        refundPlanAdapter.setNotifyDataChanged(datas);
                    }
                });
            }
        });
        planLv.setAdapter(refundPlanAdapter);
        planLv.setFocusable(false);
    }

    @Override
    protected void initData() {
        super.initData();
        RefundPlanInput planInput =
                (RefundPlanInput) getIntent().getSerializableExtra(RepayConfig.INTENT_REFUNDPLAN_INPUT);
        RefundPlanOutput retDatBean =
                (RefundPlanOutput) getIntent().getSerializableExtra(RepayConfig.INTENT_REFUNDPLAN);
        showRefundPlanView(retDatBean);

        mPresenter = new RefundPlanPresenter(this, planInput);
        mPresenter.getMccList();
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelPlanDialog();
            }
        });
        addressLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (regionCheckListener == null) {
                    regionCheckListener = new RegionCheckListener(RefundPlanActivity.this);
                }
                regionCheckListener.handleRegionCheck(v, addressTv, false, new IWheelRegionChangeCallback() {
                    @Override
                    public void wheelRegionChangeCallback(String wheelData, String wheelCode) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            addressTv.setText(wheelData);
                        }
                        mWheelCode = wheelCode;
                    }
                });
            }
        });
        repayDayLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCountChooseDialog = null;
                mCountChooseDialog = new RefundCountChooseDialog(mContext, mCurrentPaymentDays,
                        maxPaymentDays, minPaymentDays);
                mCountChooseDialog.setListener(new IRefundCountListener() {
                    @Override
                    public void onChangeRefundCount(String refundCount) {
                        //重新生成计划
                        mPresenter.updatePlanPaymentDays(repayOrderNo, refundCount);
                    }
                });
            }
        });
    }

    @OnClick({R.id.refundUpdateIv, R.id.refundStartIv})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.refundUpdateIv:
                mPresenter.makePlan();
                break;
            case R.id.refundStartIv:
                mPresenter.executePlan(repayOrderNo, mWheelCode, datas);
                break;
        }
    }

    @Override
    public void showRefundPlanView(RefundPlanOutput refundPlanOutput) {
        if (refundPlanOutput == null) return;
        repayOrderNo = refundPlanOutput.getRepayOrderNo();
        if (refundPlanOutput.isOverDueDayFlag()) {
            //超过还款日
            if (!TextUtils.isEmpty(refundPlanOutput.getOverDueDayPrompt())) {
                overDueDayPromptTv.setVisibility(View.VISIBLE);
                overDueDayPromptTv.setText(refundPlanOutput.getOverDueDayPrompt());
            }
            refundStartIv.setEnabled(false);
            refundStartIv.setBackgroundResource(R.drawable.btn_repay_disabled);
        } else {
            overDueDayPromptTv.setVisibility(View.GONE);
            refundStartIv.setEnabled(true);
            refundStartIv.setBackgroundResource(R.drawable.btn_repay_start);
        }
        if (!TextUtils.isEmpty(refundPlanOutput.getMaxPaymentDays())) {
            mCurrentPaymentDays = refundPlanOutput.getPaymentDays();
            maxPaymentDays = refundPlanOutput.getMaxPaymentDays();
            minPaymentDays = refundPlanOutput.getMinPaymentDays();
            repayDayTv.setText(mCurrentPaymentDays);
        }
        if (!TextUtils.isEmpty(refundPlanOutput.getExpectedDueStartDate()))
            refundStartTv.setText("还款时间：" + refundPlanOutput.getExpectedDueStartDate()
                    + " 至 " + refundPlanOutput.getExpectedDueEndDate());
        if (!TextUtils.isEmpty(refundPlanOutput.getCardinfo()))
            refundTv.setText("付款卡片：" + refundPlanOutput.getCardinfo() + "");
        if (!TextUtils.isEmpty(refundPlanOutput.getSettleCardInfo()))
            cardInfoTv.setText("到账卡片：" + refundPlanOutput.getSettleCardInfo() + "");
        if (!TextUtils.isEmpty(refundPlanOutput.getExpectedRepayAmount()))
            repayMoneyTv.setText("还款金额：" + refundPlanOutput.getExpectedRepayAmount() + "元");
        if (!TextUtils.isEmpty(refundPlanOutput.getCreditBalance()))
            cardBalanceTv.setText("卡内余额：" + refundPlanOutput.getCreditBalance() + "元");
        if (!TextUtils.isEmpty(refundPlanOutput.getExpectedPaymentCount()))
            repayCountTv.setText("还款笔数：" + refundPlanOutput.getExpectedPaymentCount() + "笔");
        if (!TextUtils.isEmpty(refundPlanOutput.getExpectedFee()))
            repayChargeTv.setText("总手续费：" + refundPlanOutput.getExpectedFee() + "元");
        if (refundPlanOutput.getDetailList() != null) {
            datas.clear();
            datas.addAll(refundPlanOutput.getDetailList());
            refundPlanAdapter.setNotifyDataChanged(datas);
        }
        if (!TextUtils.isEmpty(refundPlanOutput.getProvName())) {
            addressTv.setText(refundPlanOutput.getProvName() + " " + refundPlanOutput.getCityName());
            mWheelCode = refundPlanOutput.getProvCode() + " " + refundPlanOutput.getCityCode();
        }
    }

    /**
     * 放弃计划弹框
     */
    private void cancelPlanDialog() {
        DialogUtil.getInstance().dialogNoTitletTwoBtnHaveCallBack(mContext,
                "是否放弃此次还款计划", "取消", "确定",
                new DialogViewListener() {
                    @Override
                    public void btnRightConfirm() {
                        finish();
                    }

                    @Override
                    public void btnLeftCancel() {

                    }
                });
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(RefundPlanContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
